package com.tim.app.ui.activity.person_center;

import android.content.Intent;
import android.os.Parcelable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.tim.app.R;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.PrepareExchangeBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.dialog.TipDialog;
import com.tim.app.util.CashierInputFilter;
import com.tim.app.util.ToastUtil;

/**
 * Created by wdy on 2017/8/22.
 */

public class ToWxMoneyActivity extends BaseActivity {
    private ImageButton mIb_back;
    private TextView mTv_tomoney_recode;
    private EditText mEt_wx_money;
    private TextView mTv_commit;
    private ImageView mCb_check;
    private TextView tv_money_left;

    @Override
    protected int getLayoutId() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        return R.layout.activity_towxmoney;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tomoney_recode:
                startActivity(new Intent(ToWxMoneyActivity.this, WithdrawalRecordActivity.class));
                break;
            case R.id.ib_back:
                finish();
                break;
            case R.id.tv_commit:
                if (TextUtils.isEmpty(mEt_wx_money.getText().toString().trim()) || Integer.parseInt(mEt_wx_money.getText().toString().trim()) <= 0) {
                    ToastUtil.showToast("请输入提现金额");
                    return;
                }
                if (Double.parseDouble(mEt_wx_money.getText().toString().trim()) > date.getMoneyCount()) {
                    ToastUtil.showToast("超过限额");
                    return;
                }
                if (Double.parseDouble(mEt_wx_money.getText().toString().trim()) < date.getMinMoney()) {
                    ToastUtil.showToast("提现金额不能低于" + date.getMinMoney() + "元");
                    return;
                }

                goToExchangeMoney();
                break;
        }
    }

    private void goToExchangeMoney() {
        // TODO: 2017/8/22   判断
        final TipDialog tipDialog = new TipDialog(ToWxMoneyActivity.this);
        tipDialog.setTextDes(getString(R.string.withdrawal_dialog_tittle, mEt_wx_money.getText().toString().trim()));
        tipDialog.setButton1("确定", new TipDialog.DialogButtonOnClickListener() {
            @Override
            public void onClick(View button, TipDialog dialog) {
                Log.e("tag", "onClick===");
                tipDialog.dismiss();

                API_User.ins().depositMoney(TAG, UserManager.ins().getUserId(), mEt_wx_money.getText().toString().trim(), new StringResponseCallback() {
                    @Override
                    public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                        Log.e("tag", "resultValue===" + resultValue);
                        if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                            Intent intent = new Intent(ToWxMoneyActivity.this, WithdrawalApplyActivity.class);
                            intent.putExtra("money", mEt_wx_money.getText().toString().trim());
                            startActivity(intent);
                            finish();
                        }
                        return false;
                    }
                });
            }
        });
        tipDialog.setButton2("取消", new TipDialog.DialogButtonOnClickListener() {
            @Override
            public void onClick(View button, TipDialog dialog) {
                tipDialog.dismiss();

            }
        });
        tipDialog.show();
    }


    @Override
    public void initView() {
        tv_money_left = (TextView) findViewById(R.id.tv_money_left);
        mIb_back = (ImageButton) findViewById(R.id.ib_back);
        mTv_tomoney_recode = (TextView) findViewById(R.id.tv_tomoney_recode);
        mEt_wx_money = (EditText) findViewById(R.id.et_wx_money);
        InputFilter[] filter = {new CashierInputFilter()};
        mEt_wx_money.setFilters(filter);
        mTv_commit = (TextView) findViewById(R.id.tv_commit);
        mTv_commit.setOnClickListener(this);
        mCb_check = (ImageView) findViewById(R.id.cb_check);
        mIb_back.setOnClickListener(this);
        mTv_tomoney_recode.setOnClickListener(this);
    }

    private PrepareExchangeBean.ResBean.ConfigureflowermoneyBean date;

    @Override
    public void initData() {
        date = getIntent().getParcelableExtra("date");
        if (null != date) {
            tv_money_left.setText(getString(R.string.withdrawal_mun, date.getMoneyCount() + ""));
        }
    }
}
